﻿Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports QBuilder



'''<summary>
'''This is a test class for InCriteriaTest and is intended
'''to contain all InCriteriaTest Unit Tests
'''</summary>
<TestClass()> _
Public Class InCriteriaTest


    Private testContextInstance As TestContext

    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(value As TestContext)
            testContextInstance = Value
        End Set
    End Property

    <TestMethod()> _
    Public Sub InCriteria_Constructor1()
        Dim target As ICriteriaComponent = New InCriteria("FieldA", "1,2,3")
        Dim expected As String = "FieldA IN (1,2,3)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_Constructor2()
        Dim target As InCriteria = New InCriteria()
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_List_Integer()
        Dim target As ICriteriaComponent = New InCriteria("A", New Integer() {1, 2, 3})
        Dim expected As String = "A IN ('1','2','3')"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_List_String()
        Dim target As ICriteriaComponent = New InCriteria("A", New String() {"1", "2", "3"})
        Dim expected As String = "A IN ('1','2','3')"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_List_Object()
        Dim target As ICriteriaComponent = New InCriteria("A", New Object() {"1", 2, #3/11/1979#})
        Dim expected As String = "A IN ('1','2','03/11/1979 12:00:00 AM')"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_List_AddItem()
        Dim target As InCriteria = New InCriteria("A", New Integer() {1, 2, 3})
        target.AddItem("4")
        Dim expected As String = "A IN ('1','2','3','4')"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_List_AddItem_Single()
        Dim target As InCriteria = New InCriteria()
        target.LeftExpression = "A"
        target.AddItem("4")
        Dim expected As String = "A IN ('4')"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_Negate()
        Dim target As InCriteria = New InCriteria("A", New String() {"1", "2", "3"})
        target.Negate = True
        Dim expected As String = "NOT A IN ('1','2','3')"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    <ExpectedException(GetType(InvalidOperationException))>
    Public Sub InCriteria_List_Empty()
        Dim Items As New List(Of String)

        Dim target As ICriteriaComponent = New InCriteria("A", Items)
        Dim actual As String
        actual = target.GetText
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_SubQuery()

        Dim SubQuery As New SelectQuery()
        SubQuery.TableName = "SomeOtherTable"
        SubQuery.Fields.Add("B")

        Dim target As ICriteriaComponent = New InCriteria("A", SubQuery)
        Dim expected As String = "A IN (SELECT B FROM SomeOtherTable)"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' InCriteria may only have 1 field selected in a sub query
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    <ExpectedException(GetType(InvalidOperationException))>
    Public Sub InCriteria_SubQuery_TooManyFields()

        Dim SubQuery As New SelectQuery()
        SubQuery.TableName = "SomeOtherTable"
        SubQuery.Fields.Add("B")
        SubQuery.Fields.Add("C")

        Dim target As ICriteriaComponent = New InCriteria("A", SubQuery)
        Dim actual As String
        actual = target.GetText
    End Sub

    <TestMethod()> _
    Public Sub InCriteria_LeftExpression()
        Dim target As InCriteria = New InCriteria()
        target.LeftExpression = "Testing"
        Assert.AreEqual("Testing", target.LeftExpression)
    End Sub


    <TestMethod()> _
    Public Sub InCriteria_RightExpression()
        Dim target As InCriteria = New InCriteria()
        target.RightExpression = "Testing"
        Assert.AreEqual("Testing", target.RightExpression)
    End Sub
End Class
